package com.xt.tea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.tea.entity.domain.Tea;
import com.xt.tea.entity.enums.RequestResultEnum;
import com.xt.tea.entity.exception.GlobalException;
import com.xt.tea.entity.vo.Condition;
import com.xt.tea.mapper.TeaMapper;
import com.xt.tea.service.TeaService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class TeaServiceImpl extends BaseService implements TeaService {
    private final TeaMapper teaMapper;
    @Override
    public IPage<Tea> getTea(Condition<Tea> condition) {
        if (condition==null){
            throw new GlobalException(RequestResultEnum.CUSTOM_PARAM_CHECK_FAILED);
        }
        Page<Tea> page = Page.of(condition.getPageIndex(), condition.getPageSize());
        LambdaQueryWrapper<Tea> queryWrapper = new LambdaQueryWrapper<>();
        if (condition.getQueryInfo()!=null){
            if (StringUtils.isNotBlank(condition.getQueryInfo().getTeaName())){
                queryWrapper.eq(Tea::getTeaName,condition.getQueryInfo().getTeaName());
            }
        }
        return teaMapper.selectPage(page,queryWrapper);

    }

    @Override
    public List<Tea> getAllTea() {
        List<Tea> teas = teaMapper.selectList(new QueryWrapper<>());
        teas.forEach(e->e.setSelectCount(0));
        return teas;
    }

    @Override
    public Integer addTea(Tea teaVo) {
        return insert(teaMapper,teaVo);
    }

    @Override
    public Tea updateTea(Tea teaVo) {
        Tea tea = this.selectById(teaMapper, teaVo.getId());
        return update(teaMapper,teaVo,tea);
    }

    @Override
    public Tea updateTeaStatus(Integer id) {
        Tea tea = this.selectById(teaMapper, id);
        return updateDataStatusById(teaMapper,tea);
    }

    @Override
    public Integer deleteTea(Integer id) {
        Tea tea = this.selectById(teaMapper, id);
        return deleteById(teaMapper,tea);
    }
}
